// pages/my-check/my-check.js
const {request ,loginApi} = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    id: '',
    userid: '',
    user: {},
    shenfen: 0,
    remark:'',
    survey:'1'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.setData({
      id: options.id,
      userid: options.userid
    })
    if (!getApp().checkLogin()) return
  },
  getDetail(id) {
    if (this.data.userid) {
      request('/recv_acceptance', 'POST', {
        member_id: wx.getStorageSync('user').member_id,
        id: id
      }).then(res => {
        if (res.data.code == 200) {
          request('/acceptance_page', 'POST', {
            member_id:this.data.userid || wx.getStorageSync('user').member_id,
            id: id
          }).then(res => {
            console.log('id:',id)
            console.log('member_id:',wx.getStorageSync('user').member_id)
            console.log('data:',res.data.data)
            const columns = []
            if (res.data.code == 200) {
              this.setData({
                detail: res.data.data,
                survey: String(res.data.data.survey)
              })
            }
          })
        }
      })
    } else{
      request('/acceptance_page', 'POST', {
        // member_id:this.data.userid || wx.getStorageSync('user').member_id,
        id: id
      }).then(res => {
        console.log('id:',id)
        console.log('member_id:',wx.getStorageSync('user').member_id)
        console.log('data:',res.data.data)
        const columns = []
        if (res.data.code == 200) {
          this.setData({
            detail: res.data.data,
            survey: String(res.data.data.survey)
          })
        }
      })
    }
  },
  onChange(e) {
    this.setData({
      survey: e.detail
    })
  },
  onCheck() {
    
    if (!getApp().checkLogin()) return
    // if(!wx.getStorageSync('user') || !wx.getStorageSync('user').member_id){
    //   wx.navigateTo({
    //     url: '/pages/grant/grant',
    //   })
    //   return
    // }
    if(!this.data.survey){
      wx.showToast({
        title: '请选择评价',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/signature/signature?survey='+this.data.survey+'&remark='+this.data.remark+'&id='+this.data.id,
    })
    return
    request('/signature_sub', 'POST', {
      member_id: wx.getStorageSync('user').member_id,
      id: this.data.id
    }).then(res => {
      console.log(res.data.data)
      if (res.data.code == 200) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let userInfo = wx.getStorageSync('user')
    if (userInfo && userInfo.id) {
      this.setData({
        user: userInfo,
        shenfen: userInfo.member.shenfen
      })
    }
    if (this.data.user.id) {
      this.getDetail(this.data.id)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})